import mirror, {actions, connect, render} from 'mirrorx'

mirror.model({
    name: 'indexState',
    initialState: {
        banners:[],
        //推荐歌单
        recommend:[],
        //热门歌手
        artistsList:[],
        //每日推荐歌曲
        topSongs:[],
        scrollY:0
    },
    reducers: {
        getBanner( state, data = [] ){
            return {
                ...state,
                banners:data
            };
        },
        getDaySongSheet( state, data = [] ){
            return {
                ...state,
                recommend:data
            };
        },
        getArtists( state, data = [] ) {
            return {
                ...state,
                artistsList:data
            };
        },
        saveScrolly(state, y ){
            return {
                ...state,
                scrollY:y
            };
        }
    },
    effects: {
        //获取banner
         getBannerAsync: async params => (
             Ajax.get({
                 url:'/Banner'
             }).then(({ banners }) => {
                 actions.indexState.getBanner(banners);
                 return banners;
             })
        ),
        //获取每日推荐歌单
        getDaySongSheetAsync: async params => (
            Ajax.get({
                url:'/personalized?offset=1&limit=9'
            }).then(({ result })=> {
                actions.indexState.getDaySongSheet(result);
                return result;
            })
        ),
        //获取热门歌手
        getArtistsAsync: async params => (
            Ajax.get({
                url:'/top/artists?offset=1&limit=9'
            }).then(({ artists })=> {
                actions.indexState.getArtists(artists);
                return artists;
            })
        )
    }
});